using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Spinning.Spinning.Unitity;
using Spinning.Spinning.Level;
using Microsoft.Xna.Framework.Input.Touch;
using Spinning.Spinning.Entity.Particle;
using Microsoft.Devices.Sensors;

namespace Spinning.Spinning.Entity.Scenes
{
    class PracticeFireScene : PracticeScene
    {
        ParticleShoter shoter;
        Vector3 _accelReading = new Vector3();

        public override void OnLoad()
        {
            base.OnLoad();
            timeCountBar = new TimeCountBar(new Vector2(15, 20), 10, "fire");
            timeCountBar.OnGetThreePointEvent += OnGetThreePoint;
            timeCountBar.OnTimesUpEvent += DoFail;

            this.Attach(timeCountBar);

            ParticleLuncher luncher = new RealEnemyLuncher(30, this);
            UpdateHandlers.Add(luncher);
            luncher.Areas.Add(new Rectangle(-200,-200,480+200,150));
            luncher.Rate = 10;
            luncher.ParticleMax = 3;

            shoter = new ParticleShoter(this);
            UpdateHandlers.Add(shoter);

            for (int i = 0; i < luncher.Particles.Count;i++ ) {
                FireEnemy fe = (FireEnemy)luncher.Particles[i];
                fe.OnTouchEvent += onEnemyTouch;
                fe.OnBeginContactEvent += onHitEnemy;
            }

            Player.body.SetType(Box2D.XNA.BodyType.Static);
            Game1._acceleMeter.CurrentValueChanged += GSensorChanged;
            try
            {
                Game1._acceleMeter.Start();
            }
            catch (AccelerometerFailedException e) { throw e; }
            catch (UnauthorizedAccessException e) { throw e; }
        }

        void GSensorChanged(object sender, SensorReadingEventArgs<AccelerometerReading> e)
        {
            _accelReading.X = (float)e.SensorReading.Acceleration.X;
            _accelReading.Y = -(float)e.SensorReading.Acceleration.Y;
            _accelReading.Z = (float)e.SensorReading.Acceleration.Z;
        }

        protected override void UpdateSelf(GameTime gameTime)
        {
            base.UpdateSelf(gameTime);
            if (!isPause)
            {
                if (_accelReading.Y > 0.6) Player.SetChangeFrameAnimation(new int[] { 0, 1 });
                else if (_accelReading.Y < -0.2) Player.SetChangeFrameAnimation(new int[] { 4, 5 });
                else Player.SetChangeFrameAnimation(new int[] { 2, 3 });
            }
            //Player.SetChangeFrameAnimation
        }

        public void onEnemyTouch(BaseEntity entity, TouchLocation touchLocation) {
            shoter.Shot(entity.Position);
        }

        private void OnGetThreePoint()
        {
            Game1.stageManager.GoToScene("successFire");
        }

        private void onHitEnemy(PhysicSprite me, PhysicSprite _other)
        {
          
            Actor other = (Actor)_other;
            if (other.Physic_Type == Spinning.Entity.Actor.PhysicType.Marble)
            {
                Marble m = (Marble)other;
                FireEnemy fe = (FireEnemy)me;
                if (!m.isDead&&m.isEnter&&fe.isEnter)
                {
                    fe.isDead = true;
                    timeCountBar.GetPoint();
                }
            }
        }

        public override Scene Clone()
        {
            Scene scene = new PracticeFireScene();
            scene.Size = new Vector2(Constants.Window_Width, Constants.Window_Height);
            scene.OnLoad();
            scene.SortChildren();
            return scene;
        }
    }
}
